﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Threading;
using OnlineVideos.Data;
using Common.Library;
using OnlineVideos.View_Models;
using OnlineVideos.UI;
//using OnlineMovies.UserControls;
using OnlineVideos.Common;
using System.ComponentModel;
using OnlineVideos.Entities;
using OnlineVideos.ViewModels;
using OnlineVideos.Utilities;
using Common.Utilities;

namespace OnlineVideos
{
    public partial class QuestionsDisplay : PhoneApplicationPage
    {
        #region GlobalDeclaration
        TimerHelper objtime = new TimerHelper();
        private DispatcherTimer timer;
        string QuestionType = "";
        int popupdisplay = 0;
        QuizExitPopUp Quizpopup;
        #endregion

        public QuestionsDisplay()
        {
            try
            {
                InitializeComponent();
                if (AppSettings.ShowID != "0")
                {
                    LoadPivotThemes(AppSettings.ShowUniqueID);
                    Quizpopup = new QuizExitPopUp();
                    timer = new DispatcherTimer();
                    timer.Interval = TimeSpan.FromSeconds(1);
                    timer.Tick += new EventHandler(timer_Tick);
                }
            }
            catch (Exception ex)
            {

                Exceptions.SaveOrSendExceptions("Exception in QuestionsDisplay method In QuestionsDisplay.cs file.", ex);
            }
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                FlurryWP8SDK.Api.LogEvent("QuestionsDisplay Page", true);
                string timelimit = "";
                timer.Start();
                GetPageDataInBackground();
                PageHelper.RemoveEntryFromBackStack("QuestionsDisplay");
                AppSettings.ShowQuestionId = "1";
                timelimit = QuizManager.GetTimerForTest();
                string s = timelimit;
                string[] readlines2 = s.Split(':');
                objtime.time(readlines2);
            }
            catch (Exception ex)
            {
                
               Exceptions.SaveOrSendExceptions("Exception in PhoneApplicationPage_Loaded event In QuestionsDisplay.cs file.", ex);
            }


        }

        private void GetPageDataInBackground()
        {
            BackgroundHelper bwHelper = new BackgroundHelper();

            bwHelper.AddBackgroundTask(
                                        (object s, DoWorkEventArgs a) =>
                                        {
                                            a.Result = QuizManager.Getquestion();
                                        },
                                        (object s, RunWorkerCompletedEventArgs a) =>
                                        {
                                            List<QuizQuestions> question = (List<QuizQuestions>)a.Result;
                                            spQuestions.DataContext = question[0];
                                            QuizHelper.SetQuizOptionCheckedState(question[0].QuestionType, question[0].UserAnswer, "A", rbA, cbA);
                                            QuizHelper.SetQuizOptionCheckedState(question[0].QuestionType, question[0].UserAnswer, "B", rbB, cbB);
                                            QuizHelper.SetQuizOptionCheckedState(question[0].QuestionType, question[0].UserAnswer, "C", rbC, cbC);
                                            QuizHelper.SetQuizOptionCheckedState(question[0].QuestionType, question[0].UserAnswer, "D", rbD, cbD);
                                            QuestionType = question[0].QuestionType;
                                        }
                                      );
            bwHelper.RunBackgroundWorkers();
            SettingsHelper.Save("messagehighlight", "False");
        }

        void timer_Tick(object sender, EventArgs e)
        {
            try
            {
                tblktimer.Text = objtime.manipulatetime();
                if (tblktimer.Text == (new TimeSpan(0, 0, 0)).ToString())
                {
                    timer.Stop();
                    NavigationService.Navigate(NavigationHelper.getQuizresultPage(tblktimer.Text));
                }
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in timer_Tick event In QuestionsDisplay.cs file.", ex);
            }

        }

        private void LoadPivotThemes(long ShowID)
        {
            tblkVideosTitle.Text = QuizManager.GetSubjectsTitle();
        }

        private void imgTitle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            NavigationService.Navigate(NavigationHelper.MainPanoramaPage);
        }
        private void PhoneApplicationPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
        {
            try
            {
                if (Quizpopup.popMessage.IsOpen == false)
                {
                    Quizpopup.Show();
                    popupdisplay = 1;
                    e.Cancel = true;
                    base.OnBackKeyPress(e);
                }
                else
                {
                    Quizpopup.Close();
                }
            }
            catch (Exception ex)
            {
                
                Exceptions.SaveOrSendExceptions("Exception in PhoneApplicationPage_BackKeyPress method In QuestionsDisplay.cs file.", ex);
            }
        }

        private void imgprevious_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                int id = Convert.ToInt32(AppSettings.ShowQuestionId) - 1;
                int maxid = Convert.ToInt32(AppSettings.ShowMaxQuestionId) - 1;
                AppSettings.ShowQuestionId = id.ToString();
                txtmsg.Visibility = Visibility.Collapsed;
                if (AppSettings.ShowQuestionId == maxid.ToString())
                {
                    imgnext.Source = ImageHelper.GetImageForQuiz("Next");
                }
                SettingsHelper.Save("messagehighlight", "True");
                GetPageDataInBackground();
                if (AppSettings.ShowQuestionId == "1")
                {
                    imgprevious.Visibility = Visibility.Collapsed;
                }
            }
            catch (Exception ex)
            {
                
                Exceptions.SaveOrSendExceptions("Exception in imgprevious_MouseLeftButtonDown method In QuestionsDisplay.cs file.", ex);
            }
        }

        private void imgnext_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                if (AppSettings.ShowQuestionId != AppSettings.ShowMaxQuestionId)
                {
                    QuizHelper.GetOptionChecked(rbA, rbB, rbC, rbD, cbA, cbB, cbC, cbD, QuestionType);
                    if (SettingsHelper.getStringValue("messagehighlight") == "False")
                    {
                        txtmsg.Visibility = Visibility.Visible;
                    }
                    else
                    {
                        tblktimer.Margin = new Thickness(15, 20, 0, 0);
                        imgnext.Margin = new Thickness(0, 0, 0, 0);
                        txtmsg.Visibility = Visibility.Collapsed;
                        imgprevious.Visibility = Visibility.Visible;
                        int id = Convert.ToInt32(AppSettings.ShowQuestionId) + 1;
                        AppSettings.ShowQuestionId = id.ToString();
                        GetPageDataInBackground();
                    }
                    if (AppSettings.ShowMaxQuestionId == AppSettings.ShowQuestionId)
                    {
                        imgnext.Source = ImageHelper.GetImageForQuiz("Finish");
                    }
                }
                else
                {
                    timer.Stop();
                    QuizHelper.GetOptionChecked(rbA, rbB, rbC, rbD, cbA, cbB, cbC, cbD, QuestionType);
                    if (SettingsHelper.getStringValue("messagehighlight") == "False")
                    {
                        txtmsg.Visibility = Visibility.Visible;
                    }
                    else
                    {
                        NavigationService.Navigate(NavigationHelper.getQuizresultPage(SettingsHelper.getStringValue("TimeUsed")));
                    }
                }
            }
            catch (Exception ex)
            {
                
                 Exceptions.SaveOrSendExceptions("Exception in imgnext_MouseLeftButtonDown method In QuestionsDisplay.cs file.", ex);
            }
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            try
            {
                FlurryWP8SDK.Api.LogPageView();
            }
            catch (Exception ex)
            {

                Exceptions.SaveOrSendExceptions("Exception in OnNavigatedTo Method In QuestionsDisplay.cs file.", ex);
            }
        }
        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            try
            {
                FlurryWP8SDK.Api.EndTimedEvent("QuestionsDisplay Page");
                PageHelper.RemoveEntryFromBackStack("QuestionsDisplay");
            }
            catch (Exception ex)
            {
                
                  Exceptions.SaveOrSendExceptions("Exception in OnNavigatedFrom method In QuestionsDisplay.cs file.", ex);
            }


        }

    }
}










